package com.teaphy.readlist.handler;

import com.teaphy.readlist.model.MyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author teaphy
 * @desc
 * @since 21:01
 */
@RestControllerAdvice
public class CustomHandler {

    // 捕获全局异常,处理所有不可知的异常
    @ExceptionHandler(Exception.class)
    public Object handlerCustomException(Exception e, HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();

        map.put("code", "-100001");
        map.put("message", e.getMessage());
        map.put("url", request.getRequestURL());

        return map;
    }

    @ExceptionHandler(MyException.class)
    public Object handlerMyException(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error.html");
        return modelAndView;
    }
}
